<?php
declare (strict_types=1);

use think\helper\Str;

if (!function_exists('domainUrl')) {
    $domain = '';
    /**
     * 将url增加域名
     * @param string $url
     * @return string
     */
    function domainUrl(string $url)
    {
        global $domain;
        if ($domain == '') {
            $domain = sysValue('domain_name');
        }
        return $domain . $url;
    }
}

if (!function_exists('convertNumber')) {
    /**
     * 将字符转成数值型
     * @param array $vo
     */
    function convertNumber(array &$vo)
    {
        foreach ($vo as $field => $val) {
            if (Str::endsWith($field, '_price') || Str::endsWith($field, '_money')) {
                $vo[$field] = floatval($val);
            } else if (Str::endsWith($field, '_num')) {
                $vo[$field] = intval($val);
            }
        }
    }
}

if (!function_exists('provinceList')) {
    /**
     * 获取省份列表
     * @return array
     */
    function provinceList(): array
    {
        return \app\common\model\BaseArea::getAreaByParent(\app\common\model\BaseArea::T_PROVINCE);
    }
}

if (!function_exists('getCityListByProvinceId')) {
    /**
     * 根据省份ID获取城市列表
     * @param $provinceId 省份ID
     * @return array
     */
    function getCityListByProvinceId($provinceId): array
    {
        if ($provinceId == '') {
            return [];
        }
        return \app\common\model\BaseArea::getAreaByParent(\app\common\model\BaseArea::T_CITY, $provinceId);
    }
}

if (!function_exists('currentMember')) {
    /**
     * 前台获取当前会员ID
     * @return array
     */
    function currentMember()
    {
        $token = request()->server('http_authorization');
        if (!empty($token)) {
            $member = ffhome\util\JwtAuth::verifyToken($token, sysValue('token_key'));
            if (!$member) return ['id' => 0];
            if (!empty($member['id'])) {
                app\common\model\AclUser::updateLoginEveryday($member['id']);
            }
            return $member;
        }
        return ['id' => 0];
    }
}

if (!function_exists('getMiniFactory')) {
    /**
     * 获取微信小程序对象
     * @return \EasyWeChat\MiniProgram\Application
     */
    function getMiniFactory()
    {
        $config = config('wechat.mini');
        return \EasyWeChat\Factory::miniProgram($config);
    }
}